package breeze.tool.life;
 
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.ScaleAnimation;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import breeze.tool.life.activities.ExpressInspection;
import breeze.tool.life.database.AppSqlUtils;
import breeze.tool.life.utils.AppStyles;
import brz.breeze.app_utils.BActivity;
import brz.breeze.service_utils.BExceptionCatcher;
import java.util.Calendar;
import android.text.InputType;
import android.graphics.Color;
import breeze.tool.life.activities.AppSettingsActivity;
import android.app.ActivityOptions;
import breeze.tool.life.broadcasts.GaokaoTimeWidget;

public class MainActivity extends BActivity {

	private static final String TAG = "MainActivity";
	
	private TextView mGaokaoTime,mMyGrade;
	private RelativeLayout mCardBacoground;
	
	@Override
	public void init() {
		//初始化工作
		preference.edit().putFloat("aimGrade",560f).apply();
		//状态栏沉浸
		getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
		if(getActionBar()!=null){
			getActionBar().hide();
		}
		if(Build.VERSION.SDK_INT>=21){
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		}
		//获取控件
		mGaokaoTime = find(R.id.activitymainTextView_gaokao_time);
		mCardBacoground = find(R.id.activitymainRelativeLayout1);
		mMyGrade = find(R.id.layoutmaintools_my_grade);
	}

	@Override
	public void initData() {
		int countTime = GaokaoTimeWidget.getTime();
		mGaokaoTime.setText(String.valueOf(countTime)+"天");
		mCardBacoground.setBackground(AppStyles.getGaokaoTimeStyle(countTime));
		initMyGrade();
	}
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
		init_style();
		initData();
    }

	private void init_style() {
		findViewById(R.id.layoutmaintools_zhimi).setBackground(AppStyles.appIconColor("#03bd8f"));
		findViewById(R.id.layoutmaintools_bilibili).setBackground(AppStyles.appIconColor("#ff3877"));
		findViewById(R.id.layoutmaintools_express_inspect).setBackground(AppStyles.appIconColor("#3F51B5"));
		findViewById(R.id.layoutmaintools_time_year).setBackground(AppStyles.appIconColor("#F06292"));
		findViewById(R.id.layoutmaintools_time_date).setBackground(AppStyles.appIconColor("#F44336"));
		findViewById(R.id.layoutmaintools_coolapk).setBackground(AppStyles.appIconColor("#0fa15a"));
		findViewById(R.id.layoutmaintools_current_grade).setBackground(AppStyles.appIconColor("#f2f3f5"));
	}
	
	public void cdu_web(View view){
		Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.cdu.edu.cn/"));
		startActivity(intent);
	}
	
	//启动APP
	public void zhimi(View v){
		clickAnimation(v);
		if(!startApp("cn.edu.zjicm.wordsnet_d")){
			toast("启动应用失败");
		}
	}
	
	public void bilibili(View v){
		clickAnimation(v);
		if(!startApp("tv.danmaku.bili")){
			if(!startApp("com.bilibili.app.in")){
				toast("启动应用失败");
			}
		}
	}
	
	public void coolapk(View v){
		clickAnimation(v);
		if(!startApp("com.coolapk.market")){
			toast("启动应用失败");
		}
	}
	
	public void GoSettings(View v){
		clickAnimation(v);
		Intent intent = new Intent(this,AppSettingsActivity.class);
		
		View view = find(R.id.activitymainTextView1);
		String transitionName = "Title";
		ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this,view,transitionName);
		
		startActivity(intent,options.toBundle());
	}
	
	public void myGrade(View v){
		clickAnimation(v);
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("当前成绩");
		LinearLayout line = new LinearLayout(this);
		final EditText edittext = new EditText(this);
		edittext.setBackground(AppStyles.getEdittextStyle());
		LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		ll.setMargins(70,40,70,40);
		edittext.setHint("请输入当前成绩");
		edittext.setTextSize(15);
		edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
		edittext.setLayoutParams(ll);
		line.addView(edittext);
		alert.setView(line);
		alert.create().setCanceledOnTouchOutside(true);
		alert.setPositiveButton("确定", new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface p1, int p2) {
					String content = edittext.getText().toString();
					if(content!=null&&!content.isEmpty()){
						preference.edit().putFloat("myGrade",Float.parseFloat(content)).apply();
						initMyGrade();
					}else{
						toast("输入内容有误");
					}
				}
			});
		alert.setNegativeButton("取消",null);
		alert.show();
	}
	
	private void initMyGrade(){
		float grade = preference.getFloat("myGrade",100.0f);
		float aimGradr = preference.getFloat("aimGrade",0.0f);
		if(grade!=0.0f&&aimGradr!=0.0f){
			mMyGrade.setText(String.valueOf((int)grade));
			int color = 0;
			int precent = (int)((grade/aimGradr)*100);
			if(precent<=70&&precent>0){
				color = Color.parseColor("#F44336");
			}else if(precent<=98&&precent>70){
				color = Color.parseColor("#EF6C00");
			}else if(precent>98){
				color = Color.parseColor("#009688");
			}
			mMyGrade.setTextColor(color);
		}
	}
	
	public void express_inspect(View v){
		clickAnimation(v);
		startActivity(new Intent(this,ExpressInspection.class));
	}
	
	private boolean startApp(String packageName){
		try{
			Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
			startActivity(intent);
			finish();
			return true;
		}catch(Exception e){
			BExceptionCatcher.e(App.getContext(),e);
			return false;
		}
	}
	
	//按两次退出
	private boolean isExit = false;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == event.KEYCODE_BACK){
			if(!isExit){
				isExit = true;
				handle.sendEmptyMessageDelayed(0,2000);
				toast("再按一次退出");
				return false;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
	
	Handler handle = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			isExit = false;
			super.handleMessage(msg);
		}
	};
	
	private void clickAnimation(View v){
		ScaleAnimation scale = new ScaleAnimation(0.9f,1f,0.9f,1f,ScaleAnimation.RELATIVE_TO_SELF,0.5f,ScaleAnimation.RELATIVE_TO_SELF,0.5f);
		scale.setDuration(500);
		v.startAnimation(scale);
	}
	
} 
